import numpy as np
import base64
import cv2
import os


SrcDir = r"C:\Users\Administrator\Desktop\闪电文字语音转换软件\r\wav"
DstDir = r"C:\Users\Administrator\Desktop\闪电文字语音转换软件\r\txt"

for root, dirs, files in os.walk(SrcDir):
    for file in files:
        with open(root + "\\" + file, 'rb') as f:
            wav = f.read()
            # wav_base64 = base64.b64encode(wav).decode('utf-8')
            size = len(wav)
            f2 = open(DstDir + "\\" + file[:-4] + '.txt', 'w')
            for i in range(size):
                # print('0x%02x,'%wav[i])
                f2.write('0x%02X,' % wav[i])
                if i % 16 == 0:
                    f2.write('\n')
            f2.close()



# a=[5,185,32]
# a=np.uint8(a).reshape(1,1,3)
# a=cv2.cvtColor(a,cv2.COLOR_YUV2BGR)
# a=cv2.cvtColor(a,cv2.COLOR_BGR2HSV)
# print(a)
# # a=[1,5,3,6,8,2,5]
# # for index,num in enumerate(a):
# #     print(index,num)
# hashmap = {}biyan.wav
# target=9
# for index, num in enumerate(a):
#     another_num = target - num
#     if another_num in hashmap:
#       pass
#         # [hashmap[another_num], index]
#     hashmap[num] = index
# print(hashmap)
# sorted_id = sorted(range(len(a)), key=lambda k: a[k])
# print(sorted_id,a)

# a=3*64+64*64+64*128+128*128+128*128+128*128+128*256+256*256+256*256+256*512+512*512
# a=a*9/1024/1024
# print(a)
# from PIL import Image
# import cv2


# # -*- coding: utf-8 -*-
# """
# Created on Sat Mar  2 21:17:58 2019

# @author: Administrator
# """

# '''
# 图像去除噪声
# '''
# import matplotlib.pyplot as plt
# import numpy as np
# # from skimage.util import random_noise
# from PIL import Image
# import cv2


# # ###  中值滤波
# # median_filter_img = cv2.medianBlur(img, 3)
# # ax2 = fig.add_subplot(2,3,2)
# # ax2.imshow(median_filter_img)
# # plt.title('median_filter')

# # #### 高斯滤波
# # Gaussian_filter_img = cv2.GaussianBlur(img, (3,3), 0)
# # ax2 = fig.add_subplot(2,3,3)
# # ax2.imshow(Gaussian_filter_img)
# # plt.title('Gaussian_filter')

# # ####　均值滤波
# # mean_vaule_filter = cv2.blur(img, (5,5))
# # ax2 = fig.add_subplot(2,3,4)
# # ax2.imshow(mean_vaule_filter)
# # plt.title('mean_vaule_filter')

# # #### 双边滤波
# # #9 邻域直径，两个 75 分别是空间高斯函数标准差，灰度值相似性高斯函数标准差
# # blur = cv2.bilateralFilter(img,9,75,75)
# # ax2 = fig.add_subplot(2,3,5)
# # ax2.imshow(blur)
# # plt.title('bilatral-filter')


# def yuv420_to_rgb888(width, height, yuv):
#     # function requires both width and height to be multiples of 4
#     if (width % 4) or (height % 4):
#         raise Exception("width and height must be multiples of 4")
#     rgb_bytes = bytearray(width*height*3)

#     red_index = 0
#     green_index = 1
#     blue_index = 2
#     y_index = 0

#     for row in range(0,height):
#         u_index = width * height + (row//2)*(width//2)
#         v_index = u_index + (width*height)//4

#         for column in range(0,width):
#             Y = yuv[y_index]
#             U = yuv[u_index]
#             V = yuv[v_index]
#             # U =   yuv[v_index]
#             # V =  yuv[u_index]
#             C = (Y - 16) * 298
#             D = U - 128
#             E = V - 128
#             R = (C + 409*E + 128) // 256
#             G = (C - 100*D - 208*E + 128) // 256
#             B = (C + 516 * D + 128) // 256

#             R = 255 if (R > 255) else (0 if (R < 0) else R)
#             G = 255 if (G > 255) else (0 if (G < 0) else G)
#             B = 255 if (B > 255) else (0 if (B < 0) else B)

#             rgb_bytes[red_index] = R
#             rgb_bytes[green_index] = G
#             rgb_bytes[blue_index] = B

#             u_index += (column % 2)
#             v_index += (column % 2)
#             y_index += 1

#             red_index += 3
#             green_index += 3
#             blue_index += 3

#     return rgb_bytes

# def yuv420_to_rgb8881(width, height, yuv):
#     # function requires both width and height to be multiples of 4
#     if (width % 4) or (height % 4):
#         raise Exception("width and height must be multiples of 4")
#     rgb_bytes = bytearray(width*height*3)

#     red_index = 0
#     green_index = 1
#     blue_index = 2
#     y_index = 0
#     u_index=0
#     v_index=0
#     print(len(yuv))
#     for row in range(0,height):
#         # u_index = width * height + (row//2)*(width//2)
#         # v_index = u_index + (width*height)//4
#         for column in range(0,width):


#             u_index=width * height+((row//2)*(width//2)+column//2)*2
#             v_index=width * height+((row//2)*(width//2)+column//2)*2+1
#             # if row<4 and column<4:
#             #     print(u_index,v_index)
#             # print(u_index,v_index)
#             Y = yuv[y_index]
#             U = yuv[u_index]
#             V = yuv[v_index]
#             # y_index+=1
#             # C = (Y - 16) * 298
#             # D = U - 128
#             # E = V - 128
#             # R = (C + 409*E + 128) // 256
#             # G = (C - 100*D - 208*E + 128) // 256
#             # B = (C + 517 * D + 128) // 256
#             R = Y + ((360 * (V - 128))//256)
#             G =Y - (( ( 88 * (U - 128)  + 184 * (V - 128)) )//256)
#             B =  Y +((455 * (U - 128))//256)

#             R = 255 if (R > 255) else (0 if (R < 0) else R)
#             G = 255 if (G > 255) else (0 if (G < 0) else G)
#             B = 255 if (B > 255) else (0 if (B < 0) else B)

#             rgb_bytes[red_index] = R
#             rgb_bytes[green_index] = G
#             rgb_bytes[blue_index] = B

#             u_index += (column % 2)
#             v_index += (column % 2)
#             y_index += 1

#             red_index += 3
#             green_index += 3
#             blue_index += 3

#     return rgb_bytes
# def y_to_rgb888(width, height, yuv):
#     # function requires both width and height to be multiples of 4
#     if (width % 4) or (height % 4):
#         raise Exception("width and height must be multiples of 4")
#     rgb_bytes = bytearray(width*height*3)

#     red_index = 0
#     green_index = 1
#     blue_index = 2
#     y_index = 0
#     u_index=0
#     v_index=0
#     print(len(yuv))
#     for row in range(0,height):
#         # u_index = width * height + (row//2)*(width//2)
#         # v_index = u_index + (width*height)//4
#         for column in range(0,width):


#             u_index=width * height+((row//2)*(width//2)+column//2)*2
#             v_index=width * height+((row//2)*(width//2)+column//2)*2+1
#             # if row<4 and column<4:
#             #     print(u_index,v_index)
#             # print(u_index,v_index)
#             Y = yuv[y_index]
#             U = yuv[u_index]
#             V = yuv[v_index]
#             # y_index+=1
#             # C = (Y - 16) * 298
#             # D = U - 128
#             # E = V - 128
#             # R = (C + 409*E + 128) // 256
#             # G = (C - 100*D - 208*E + 128) // 256
#             # B = (C + 517 * D + 128) // 256
#             R = Y 
#             G =Y
#             B =  Y 

#             R = 255 if (R > 255) else (0 if (R < 0) else R)
#             G = 255 if (G > 255) else (0 if (G < 0) else G)
#             B = 255 if (B > 255) else (0 if (B < 0) else B)

#             rgb_bytes[red_index] = R
#             rgb_bytes[green_index] = G
#             rgb_bytes[blue_index] = B

#             u_index += (column % 2)
#             v_index += (column % 2)
#             y_index += 1

#             red_index += 3
#             green_index += 3
#             blue_index += 3

#     return rgb_bytes
# def equal_hist(image):
#     image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
#     equ = cv2.equalizeHist(image)
#     plt.hist(image.ravel(), 256)
#     plt.hist(equ.ravel(), 256)
#     plt.show()
# def testConversion(source,dest):
#     print(source)
#     print("opening file")
#     f = open(source, "rb")
#     yuv = f.read()
#     f.close()
#     print("read file")
#     # rgb_bytes=y_to_rgb888(600,600, yuv)
#     rgb = yuv420_to_rgb8881(600,600, yuv)
#     img=np.uint8(rgb).reshape(600,600,3)
#     img=img[:,:,::-1]
#     img=cv2.resize(img,(100,100))
#     img1=cv2.resize(img,(600,600))
#     # mean_vaule_filter = cv2.blur(img, (5,5))
#     Gaussian_filter_img = cv2.GaussianBlur(img, (5,5), 0)
#     median_filter_img = cv2.medianBlur(Gaussian_filter_img, 3)

#     median_filter_img=cv2.resize(median_filter_img,(600,600))
#     # sobelx = cv2.Sobel(median_filter_img,cv2.CV_64F, 1, 1, ksize=3)
#     # Gaussian_filter_img = cv2.GaussianBlur(mean_vaule_filter, (5,5), 0)
#     # fastNlMeansDenoisingColored=cv2.fastNlMeansDenoisingColored(img, 15, 15, 10, 30)
#     cv2.imshow('sss',img)
#     cv2.imshow('ssss',img1)
#     # cv2.imshow('ss1sss',Gaussian_filter_img)
#     cv2.imshow('a',median_filter_img)
#     # cv2.imshow('as',fastNlMeansDenoisingColored)
#     # equal_hist(img)
#     cv2.waitKey(0)

# if __name__ == "__main__":
#     for i in range(100):
#         i=i*1
#         i=i+200            
#         # img=cv2.imread("F:\\output_img\\"+'input_img_0301.bmp')
#         # img1=cv2.imread("F:\\output_img\\"+'input_img_%04d.bmp'%i)
#         testConversion("G:\\output_img\\"+'input_img_yuv%04d.bmp'%i,'test2.jpg')

# # import cv2
# # from PIL import Image
# # from PIL import ImageChops
# # import numpy as np
# # import time
# # import pytesseract
# # import warnings
# # img=cv2.imread("test2.jpg")
# # cv2.imshow('asss',img)
# # img=img[300:350,200:250,:]
# # print(*img)
# # cv2.imshow('sss',img)
# # cv2.waitKey(0)
# # img=cv2.resize(img,(50,600))
# # warnings.filterwarnings("ignore")
# # demo=Image.open("test2.jpg")
# # im=np.array(demo.convert('L'))#灰度化矩阵
# # print(im.shape)
# # print(im.dtype)
# # #print(im)
# # height=im.shape[0]#尺寸
# # width=im.shape[1]
# # varlist=[]
# # for i in range(height):
# #     for j in range(width):
# #         for k in range(16):
# #             if im[i][j]>=k*16 and im[i][j]<(k+1)*16:#16级量化
# #                 im[i][j]=8*(k*2+1)
# #                 break
# # for i in range(0,height-height%3,3):
# #     for j in range(0,width-width%3,3):
# #         x=(im[i][j]+im[i+1][j]+im[i+2][j]+im[i][j+1]+im[i+1][j+1]+im[i+2][j+1]+im[i][j+2]+im[i+1][j+2]+im[i+2][j+2])/9
# #         x2=(pow(im[i][j],2)+pow(im[i+1][j],2)+pow(im[i+2][j],2)+pow(im[i][j+1],2)+pow(im[i+1][j+1],2)+pow(im[i+2][j+1],2)+pow(im[i][j+2],2)+pow(im[i+1][j+2],2)+pow(im[i+2][j+2],2))/9
# #         var=x2-pow(x,2)
# #         varlist.append(round(var,3))#子窗口的方差值3x3
# # print(im.shape)

# # #print(varlist)
# # T=round(sum(varlist)/len(varlist),3)#保留3位小数
# # print(T)
